﻿using System;
using System.Text;
using TI_BLE_HCI_ClientLib.BaseEntities;
using TI_BLE_HCI_ClientLib.GenericHCIEntities;
using TI_BLE_HCI_ClientLib.HCIBaseEntities;

namespace TI_BLE_HCI_ClientLib.HCICommands
{
    public class HCICommand_GATT_Write_Characteristic_Value_Request : HCIVendorSpecificCommand
    {
        private const byte CMD = 0x12;

        public static byte CommandOpCode
        {
            get { return CMD; }
        }

        public HCICommand_GATT_Write_Characteristic_Value_Request(ushort connHandle, ushort charHandle, byte[] value)
            : base(HCIVendorSpecificCommandCSG.GATT,CMD)
        {
            var pars = new BaseEntityParameter[3];

            pars[0] = new WordParameter(connHandle);
            pars[1] = new WordParameter(charHandle);
            pars[2] = new ByteArrayParameter(value);
            
            base.addParameters(pars);
        }

        public HCICommand_GATT_Write_Characteristic_Value_Request(ushort connHandle, ushort charHandle, byte value)
            : this(connHandle,charHandle,new byte[] {value}) { }

        public HCICommand_GATT_Write_Characteristic_Value_Request(ushort connHandle, ushort charHandle, ushort value)
            : this(connHandle, charHandle, new byte[] { (byte)(value & 0xFF),(byte)((value>>8) & 0xFF)  }) { }

        public HCICommand_GATT_Write_Characteristic_Value_Request(ushort connHandle, ushort charHandle, string value)
            : this(connHandle, charHandle, Encoding.UTF8.GetBytes(value)) { }
    }
}
